|
1
|
|
|
import chai from 'chai'; |
|
2
|
|
|
import chaiaspromised from 'chai-as-promised'; |
|
3
|
|
|
import nock from 'nock'; |
|
4
|
|
|
import sinon from 'sinon'; |
|
5
|
|
|
import sinonchai from 'sinon-chai'; |
|
6
|
|
|
|
|
7
|
|
|
import cgapi from '../../src/codingame/api.js'; |
|
8
|
|
|
import cgparse from '../../src/codingame/parse.js'; |
|
9
|
|
|
import CodingameError from '../../src/codingame/error.js'; |
|
10
|
|
|
|
|
11
|
|
|
let expect = chai.expect; |
|
12
|
|
|
chai.use(chaiaspromised); |
|
13
|
|
|
chai.use(sinonchai); |
|
14
|
|
|
|
|
15
|
|
View Code Duplication |
describe(`[module] codingame/api`, function() { |
|
|
|
|
|
|
16
|
|
|
describe(`[method] login`, function() { |
|
17
|
|
|
let credentialsok = { |
|
18
|
|
|
"username": `me`, |
|
19
|
|
|
"password": `p4ssw0rd` |
|
20
|
|
|
}; |
|
21
|
|
|
let credentialsnotok = { |
|
22
|
|
|
"username": `me`, |
|
23
|
|
|
"password": `password` |
|
24
|
|
|
}; |
|
25
|
|
|
let response = { |
|
26
|
|
|
"success": true |
|
27
|
|
|
}; |
|
28
|
|
|
before(function() { |
|
29
|
|
|
nock(`https://www.codingame.com`) |
|
30
|
|
|
.post(`/services/CodingamerRemoteService/loginSiteV2`, [credentialsok.username, credentialsok.password, true]) |
|
31
|
|
|
.reply(200, response) |
|
32
|
|
|
.post(`/services/CodingamerRemoteService/loginSiteV2`, [credentialsnotok.username, credentialsnotok.password, true]) |
|
33
|
|
|
.reply(403); |
|
34
|
|
|
}); |
|
35
|
|
|
after(function() { |
|
36
|
|
|
nock.cleanAll(); |
|
37
|
|
|
}); |
|
38
|
|
|
it(`should resolve with correct username and password`, function() { |
|
39
|
|
|
let login = cgapi.login(credentialsok.username, credentialsok.password); |
|
40
|
|
|
return expect(login).to.be.fulfilled; |
|
41
|
|
|
}); |
|
42
|
|
|
it(`should reject with incorrect password`, function() { |
|
43
|
|
|
let login = cgapi.login(credentialsnotok.username, credentialsnotok.password); |
|
44
|
|
|
return expect(login).to.be.rejected; |
|
45
|
|
|
}); |
|
46
|
|
|
}); |
|
47
|
|
|
describe(`[method] test`, function() { |
|
48
|
|
|
let exercise = `5711567e959cf54dd2dd79c1b4c259560d6ba46`; |
|
49
|
|
|
let bundle = `print('1')`; |
|
50
|
|
|
let expected = `1`; |
|
|
|
|
|
|
51
|
|
|
let found = `-`; |
|
|
|
|
|
|
52
|
|
|
let language = `Python`; |
|
53
|
|
|
let testindex = 1; |
|
54
|
|
|
let body = [ |
|
55
|
|
|
exercise, |
|
56
|
|
|
{ |
|
57
|
|
|
"code": bundle, |
|
58
|
|
|
"programmingLanguageId": language, |
|
59
|
|
|
"multipleLanguages": { |
|
60
|
|
|
"testIndex": testindex |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
]; |
|
64
|
|
|
let meta = { |
|
65
|
|
|
"exercise": exercise, |
|
66
|
|
|
"test": testindex, |
|
67
|
|
|
"language": language, |
|
68
|
|
|
"bundle": bundle |
|
69
|
|
|
}; |
|
70
|
|
|
let sandbox; |
|
71
|
|
|
beforeEach(function() { |
|
72
|
|
|
sandbox = sinon.sandbox.create(); |
|
73
|
|
|
}); |
|
74
|
|
|
afterEach(function() { |
|
75
|
|
|
sandbox.restore(); |
|
76
|
|
|
nock.cleanAll(); |
|
77
|
|
|
}); |
|
78
|
|
|
it(`should resolve with metadata if test has succeeded`, function() { |
|
79
|
|
|
let parse = sandbox.stub(cgparse, `parse`).callsFake(function() { |
|
|
|
|
|
|
80
|
|
|
return Promise.resolve(meta); |
|
81
|
|
|
}) |
|
82
|
|
|
nock(`https://www.codingame.com`) |
|
83
|
|
|
.post(`/services/TestSessionRemoteService/play`, body) |
|
84
|
|
|
.reply(200, {}); |
|
85
|
|
|
let test = cgapi.test(exercise, testindex, language, bundle); |
|
86
|
|
|
return expect(test).to.be.fulfilled |
|
87
|
|
|
.and.to.eventually.be.deep.equal(meta); |
|
88
|
|
|
}); |
|
89
|
|
|
it(`should reject with CodingameError if response is ok but test failed`, function() { |
|
90
|
|
|
let message = `Error message`; |
|
91
|
|
|
let parse = sandbox.stub(cgparse, `parse`).callsFake(function() { |
|
|
|
|
|
|
92
|
|
|
let error = new CodingameError(message); |
|
93
|
|
|
return Promise.reject(error); |
|
94
|
|
|
}) |
|
95
|
|
|
nock(`https://www.codingame.com`) |
|
96
|
|
|
.post(`/services/TestSessionRemoteService/play`, body) |
|
97
|
|
|
.reply(200, {}); |
|
98
|
|
|
let test = cgapi.test(exercise, testindex, language, bundle); |
|
99
|
|
|
return expect(test).to.be.rejected |
|
100
|
|
|
.and.to.eventually.be.an.instanceof(CodingameError); |
|
101
|
|
|
}); |
|
102
|
|
|
it(`should reject with Error if parsing failed`, function() { |
|
103
|
|
|
let message = `Error message`; |
|
104
|
|
|
let parse = sandbox.stub(cgparse, `parse`).callsFake(function() { |
|
|
|
|
|
|
105
|
|
|
let error = new Error(message); |
|
106
|
|
|
return Promise.reject(error); |
|
107
|
|
|
}) |
|
108
|
|
|
nock(`https://www.codingame.com`) |
|
109
|
|
|
.post(`/services/TestSessionRemoteService/play`, body) |
|
110
|
|
|
.reply(200, {}); |
|
111
|
|
|
let test = cgapi.test(exercise, testindex, language, bundle); |
|
112
|
|
|
return expect(test).to.be.rejected |
|
113
|
|
|
.and.to.eventually.be.an.instanceof(Error); |
|
114
|
|
|
}); |
|
115
|
|
|
it(`should reject because server returned an HTTP error code`, function() { |
|
116
|
|
|
nock(`https://www.codingame.com`) |
|
117
|
|
|
.post(`/services/TestSessionRemoteService/play`, body) |
|
118
|
|
|
.reply(403, {}); |
|
119
|
|
|
let test = cgapi.test(exercise, testindex, language, bundle); |
|
120
|
|
|
return expect(test).to.be.rejected; |
|
121
|
|
|
}); |
|
122
|
|
|
}); |
|
123
|
|
|
}); |
|
124
|
|
|
|